package com.example.eeuse.model;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Comparator;
import java.util.List;

@NoArgsConstructor
@Data
public class VedaResponse {
    @JsonProperty("AIRPORT")
    private List<AIRPORTDTO> airport;
    @JsonProperty("CANCACHE")
    private Boolean cancache;
    @JsonProperty("CITY")
    private List<CITYDTO> city;
    @JsonProperty("CREATETIME")
    private Long createtime;
    @JsonProperty("ID")
    private String id;
    @JsonProperty("PLANE")
    private List<PLANEDTO> plane;
    @JsonProperty("SEGMENT")
    private List<SEGMENTDTO> segment;

    public String id()
    {
        StringBuilder sb=new StringBuilder();
        airport.stream().sorted(Comparator.comparing(AIRPORTDTO::id)).forEach(v->sb.append(v.id()));
        sb.append(cancache).append("\n");
        city.stream().sorted(Comparator.comparing(CITYDTO::id)).forEach(v->sb.append(v.id()));
        sb.append(createtime).append("\n");
        sb.append(id).append("\n");
        plane.stream().sorted(Comparator.comparing(PLANEDTO::id)).forEach(v->sb.append(v.id()));
        segment.stream().sorted(Comparator.comparing(SEGMENTDTO::id)).forEach(v->sb.append(v.id()));
        sb.append("\n");
        return sb.toString();
    }
    @NoArgsConstructor
    @Data
    public static class AIRPORTDTO {
        @JsonProperty("CITY")
        private String city;
        @JsonProperty("CODE")
        private String code;
        @JsonProperty("ENNAME")
        private String enname;
        @JsonProperty("ZHNAME")
        private String zhname;
        public String id()
        {
            return city+"_"+code+"_"+enname+"_"+zhname+"\n";
        }
    }

    @NoArgsConstructor
    @Data
    public static class CITYDTO {
        @JsonProperty("CODE")
        private String code;
        @JsonProperty("ENNAME")
        private String enname;
        @JsonProperty("ZHNAME")
        private String zhname;
        public String id()
        {
            return code+"_"+enname+"_"+zhname+"\n";
        }
    }

    @NoArgsConstructor
    @Data
    public static class PLANEDTO {
        @JsonProperty("AIRPORTTAX")
        private String airporttax;
        @JsonProperty("CODE")
        private String code;
        @JsonProperty("ENNAME")
        private String enname;
        @JsonProperty("ZHNAME")
        private String zhname;
        public String id()
        {
            return airporttax+"_"+code+"_"+enname+"_"+zhname+"\n";
        }
    }

    @NoArgsConstructor
    @Data
    public static class SEGMENTDTO {
        @JsonProperty("ADULTFUELTAX")
        private String adultfueltax;
        @JsonProperty("ARRCITY")
        private String arrcity;
        @JsonProperty("CHILDFUELTAX")
        private String childfueltax;
        @JsonProperty("DATE")
        private String date;
        @JsonProperty("DATEFLIGHT")
        private DATEFLIGHTDTO dateflight;
        @JsonProperty("DEPCITY")
        private String depcity;
        @JsonProperty("GMPFUELTAX")
        private String gmpfueltax;
        @JsonProperty("INFANTFUELTAX")
        private String infantfueltax;
        @JsonProperty("JCPFUELTAX")
        private String jcpfueltax;

        public String id()
        {
            StringBuilder sb=new StringBuilder();
            sb.append(adultfueltax+"_");
            sb.append(arrcity+"_");
            sb.append(childfueltax+"_");
            sb.append(date+"\n");
            sb.append(dateflight.id());
            sb.append(depcity+"_");
            sb.append(gmpfueltax+"_");
            sb.append(infantfueltax+"_");
            sb.append(jcpfueltax+"\n");
            return sb.toString();
        }

        @NoArgsConstructor
        @Data
        public static class DATEFLIGHTDTO {
            @JsonProperty("FBASIC")
            private FBASICDTO fbasic;
            @JsonProperty("FLIGHT")
            private List<FLIGHTDTO> flight;
            @JsonProperty("JBASIC")
            private JBASICDTO jbasic;
            @JsonProperty("WBASIC")
            private WBASICDTO wbasic;
            @JsonProperty("YBASIC")
            private YBASICDTO ybasic;

            public String id()
            {
                StringBuilder sb=new StringBuilder();
                sb.append(fbasic.id());
                flight.stream().sorted(Comparator.comparing(FLIGHTDTO::id)).forEach(v->sb.append(v.id()));
                sb.append(jbasic.id());
                sb.append(wbasic.id());
                sb.append(ybasic.id());
                return sb.toString();
            }

            @NoArgsConstructor
            @Data
            public static class FBASICDTO {
                @JsonProperty("ADULTPRICE")
                private Integer adultprice;
                @JsonProperty("FAREREFERENCE")
                private String farereference;
                @JsonProperty("NAME")
                private String name;
                public String id()
                {
                    return adultprice+"_"+farereference+"_"+name+"\n";
                }
            }

            @NoArgsConstructor
            @Data
            public static class JBASICDTO {
                @JsonProperty("ADULTPRICE")
                private Integer adultprice;
                @JsonProperty("FAREREFERENCE")
                private String farereference;
                @JsonProperty("NAME")
                private String name;
                public String id()
                {
                    return adultprice+"_"+farereference+"_"+name+"\n";
                }
            }

            @NoArgsConstructor
            @Data
            public static class WBASICDTO {
                @JsonProperty("ADULTPRICE")
                private Integer adultprice;
                @JsonProperty("FAREREFERENCE")
                private String farereference;
                @JsonProperty("NAME")
                private String name;
                public String id()
                {
                    return adultprice+"_"+farereference+"_"+name+"\n";
                }
            }

            @NoArgsConstructor
            @Data
            public static class YBASICDTO {
                @JsonProperty("ADULTPRICE")
                private Integer adultprice;
                @JsonProperty("FAREREFERENCE")
                private String farereference;
                @JsonProperty("NAME")
                private String name;

                public String id()
                {
                    return adultprice+"_"+farereference+"_"+name+"\n";
                }
            }

            @NoArgsConstructor
            @Data
            public static class FLIGHTDTO {
                @JsonProperty("AIRLINE")
                private String airline;
                @JsonProperty("ARRDATE")
                private String arrdate;
                @JsonProperty("ARRIVALTERMINAL")
                private String arrivalterminal;
                @JsonProperty("ARRPORT")
                private String arrport;
                @JsonProperty("ARRTIME")
                private String arrtime;
                @JsonProperty("CABIN")
                private List<CABINDTO> cabin;
                @JsonProperty("CODESHARE")
                private String codeshare;
                @JsonProperty("CODESHAREINFO")
                private String codeshareinfo;
                @JsonProperty("DEPARTURETERMINAL")
                private String departureterminal;
                @JsonProperty("DEPDATE")
                private String depdate;
                @JsonProperty("DEPPORT")
                private String depport;
                @JsonProperty("DEPTIME")
                private String deptime;
                @JsonProperty("FLIGHTNO")
                private String flightno;
                @JsonProperty("HALFROUNDCABIN")
                private List<HALFROUNDCABINDTO> halfroundcabin;
                @JsonProperty("MEAL")
                private String meal;
                @JsonProperty("PLANE")
                private String plane;
                @JsonProperty("RATE")
                private String rate;
                @JsonProperty("ROUNDTRIPCABIN")
                private String roundtripcabin;
                @JsonProperty("SEGINTERVAL")
                private String seginterval;
                @JsonProperty("STOPNUMBER")
                private String stopnumber;
                @JsonProperty("TERM")
                private String term;
                @JsonProperty("TIMEDURINGFLIGHT")
                private String timeduringflight;
                @JsonProperty("TIMEDURINGFLIGHT_en")
                private String timeduringflightEn;
                @JsonProperty("MEALINFO")
                private List<MEALINFODTO> mealinfo;

                public String id()
                {
                    StringBuilder sb=new StringBuilder();
                    sb.append(airline);
                    sb.append(arrdate);
                    sb.append(arrivalterminal);
                    sb.append(arrport);
                    sb.append(arrtime);
                    cabin.stream().sorted(Comparator.comparing(CABINDTO::id)).forEach(v->sb.append(v.id()));
                    sb.append(codeshare);
                    sb.append(codeshareinfo);
                    sb.append(departureterminal);
                    sb.append(depdate);
                    sb.append(depport);
                    sb.append(deptime);
                    sb.append(flightno);
                    halfroundcabin.stream().sorted(Comparator.comparing(HALFROUNDCABINDTO::id)).forEach(v->sb.append(v.id()));
                    sb.append(meal);
                    sb.append(plane);
                    sb.append(rate);
                    sb.append(roundtripcabin);
                    sb.append(seginterval);
                    sb.append(stopnumber);
                    sb.append(term);
                    sb.append(timeduringflight);
                    sb.append(timeduringflightEn);
                    if(mealinfo!=null) {
                        mealinfo.stream().sorted(Comparator.comparing(MEALINFODTO::id)).forEach(v -> sb.append(v.id()));
                    }
                    return sb.toString();

                }
                @NoArgsConstructor
                @Data
                public static class CABINDTO {
                    @JsonProperty("ADULTBAGGAGEALLOWANCE")
                    private String adultbaggageallowance;
                    @JsonProperty("ADULTBAGGAGEALLOWANCEUNIT")
                    private String adultbaggageallowanceunit;
                    @JsonProperty("ADULTFAREBASIS")
                    private String adultfarebasis;
                    @JsonProperty("ADULTFARECODE")
                    private String adultfarecode;
                    @JsonProperty("ADULTFARERULE")
                    private String adultfarerule;
                    @JsonProperty("ADULTPRICE")
                    private Integer adultprice;
                    @JsonProperty("BASICCABIN")
                    private String basiccabin;
                    @JsonProperty("BRANDTYPE")
                    private String brandtype;
                    @JsonProperty("CHILDBAGGAGEALLOWANCE")
                    private String childbaggageallowance;
                    @JsonProperty("CHILDBAGGAGEALLOWANCEUNIT")
                    private String childbaggageallowanceunit;
                    @JsonProperty("CHILDFAREBASIS")
                    private String childfarebasis;
                    @JsonProperty("CHILDFARECODE")
                    private String childfarecode;
                    @JsonProperty("CHILDFAREREFERENCE")
                    private String childfarereference;
                    @JsonProperty("CHILDFARERULE")
                    private String childfarerule;
                    @JsonProperty("CHILDPRICE")
                    private Integer childprice;
                    @JsonProperty("CHILDPRICETYPE")
                    private String childpricetype;
                    @JsonProperty("CODE")
                    private String code;
                    @JsonProperty("CODESHARERULE")
                    private String codesharerule;
                    @JsonProperty("DISCOUNT")
                    private String discount;
                    @JsonProperty("FAREREFERENCE")
                    private String farereference;
                    @JsonProperty("GBADULTPRICE")
                    private Integer gbadultprice;
                    @JsonProperty("GBCHILDPRICE")
                    private Integer gbchildprice;
                    @JsonProperty("GBINFANTPRICE")
                    private Integer gbinfantprice;
                    @JsonProperty("INFANTFAREBASIS")
                    private String infantfarebasis;
                    @JsonProperty("INFANTFARECODE")
                    private String infantfarecode;
                    @JsonProperty("INFANTFAREREFERENCE")
                    private String infantfarereference;
                    @JsonProperty("INFANTFARERULE")
                    private String infantfarerule;
                    @JsonProperty("INFO")
                    private String info;
                    @JsonProperty("MILEAGESTANDARD")
                    private Integer mileagestandard;
                    @JsonProperty("NAME")
                    private String name;
                    @JsonProperty("originalPrice")
                    private Integer originalPrice;
                    @JsonProperty("UPGRADELEG")
                    private Double upgradeleg;

                    public String id()
                    {
                        StringBuilder sb=new StringBuilder();
                        sb.append(adultbaggageallowance+"_");
                        sb.append(adultbaggageallowanceunit+"_");
                        sb.append(adultfarebasis+"_");
                        sb.append(adultfarecode+"_");
                        sb.append(adultfarerule+"_");
                        sb.append(adultprice+"_");
                        sb.append(basiccabin+"_");
                        sb.append(brandtype+"_");
                        sb.append(childbaggageallowance+"_");
                        sb.append(childbaggageallowanceunit+"_");
                        sb.append(childfarebasis+"_");
                        sb.append(childfarecode+"_");
                        sb.append(childfarereference+"_");
                        sb.append(childfarerule+"_");
                        sb.append(childprice+"_");
                        sb.append(childpricetype+"_");
                        sb.append(code+"_");
                        sb.append(codesharerule+"_");
                        sb.append(discount+"_");
                        sb.append(farereference+"_");
                        sb.append(gbadultprice+"_");
                        sb.append(gbchildprice+"_");
                        sb.append(gbinfantprice+"_");
                        sb.append(infantfarebasis+"_");
                        sb.append(infantfarecode+"_");
                        sb.append(infantfarereference+"_");
                        sb.append(infantfarerule+"_");
                        sb.append(info+"_");
                        sb.append(mileagestandard+"_");
                        sb.append(name+"_");
                        sb.append(originalPrice+"_");
                        sb.append(upgradeleg).append("\n");
                        return sb.toString();
                    }
                }

                @NoArgsConstructor
                @Data
                public static class HALFROUNDCABINDTO {
                    @JsonProperty("INFO")
                    private String info;
                    @JsonProperty("NAME")
                    private String name;
                    public String id()
                    {
                        return info+"_"+name+"\n";
                    }
                }

                @NoArgsConstructor
                @Data
                public static class MEALINFODTO {
                    @JsonProperty("ARR")
                    private String arr;
                    @JsonProperty("DEP")
                    private String dep;
                    @JsonProperty("MEAL")
                    private List<MEALDTO> meal;

                    public String id()
                    {
                        StringBuilder sb=new StringBuilder();
                        meal.stream().sorted(Comparator.comparing(MEALDTO::id)).forEach(v->sb.append(v.id()));
                        return arr+"_"+dep+"_"+sb;
                    }

                    @NoArgsConstructor
                    @Data
                    public static class MEALDTO {
                        @JsonProperty("CABIN")
                        private String cabin;
                        @JsonProperty("MEALNAMEEN")
                        private String mealnameen;
                        @JsonProperty("MEALNAMEZH")
                        private String mealnamezh;
                        public String id()
                        {
                            return cabin+"_"+mealnameen+"_"+mealnamezh+"\n";
                        }
                    }
                }
            }
        }
    }
}
